// Package requests 处理请求数据和表单验证
package requests

import (
	"github.com/gofiber/fiber/v2"
	"github.com/thedevsaddam/govalidator"
)

type DriverRequest struct {
	Year                  uint   `json:"year" valid:"year"`
	IdCard                string `json:"id_card" valid:"id_card"`
	LicenseNumber         string `json:"license_number" valid:"license_number"`
	DriverNumber          string `json:"driver_number" valid:"driver_number"`
	LicenseType           string `json:"license_type" valid:"license_type"`
	LicenseExpirationDate string `json:"license_expiration_date,omitempty" valid:"license_expiration_date"`
	EmergencyContactName  string `json:"emergency_contact_name" valid:"emergency_contact_name"`
	EmergencyContactPhone string `json:"emergency_contact_phone" valid:"emergency_contact_phone"`
}

func ValidateDriver(c *fiber.Ctx, request *DriverRequest) map[string][]string {
	c.BodyParser(request)
	rules := govalidator.MapData{
		"year":                    []string{"required", "numeric"},
		"license_number":          []string{"required"},
		"driver_number":           []string{"required"},
		"license_type":            []string{"required", "in:C1,C2,C3,B1,B2,A1,A2,A3,D,E,F"},
		"license_expiration_date": []string{"datetime"},
		"emergency_contact_name":  []string{"max_cn:1"},
		"emergency_contact_phone": []string{"phone"},
	}

	messages := govalidator.MapData{
		"year": []string{
			"required:请填写驾龄",
			"number:类型错误",
		},
		"driver_number": []string{
			"required:请填写行驶证编号",
			"len:行驶证编号格式错误",
		},
		"license_number": []string{
			"required:请填写驾驶证编号",
			"len:驾驶证编号格式错误",
		},
		"license_type": []string{
			"required:请选择驾驶证类型",
		},
	}

	return Validate(request, rules, messages)
}
